﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Petroleum.Tool;
using Petroleum.DB.Model;
using Petroleum.Model;
using HZH_Controls.Forms;
using Newtonsoft.Json;
using System.Data.SQLite;
using System.Reflection;
using HZH_Controls.Controls;
using PetroleumTool.BaseTool;

namespace Petroleum.UC
{
    public partial class Step8EstimatedMaterialConsumptionAndCostDesign : UserControl
    {
        FormMain main = new FormMain();
        /// <summary>
        /// 套管List
        /// </summary>
        List<Casing> casingList = Session.CasingProgram.CasingList;
        List<DeapthWell> depthList = new List<DeapthWell>();
        Casing casing;
        public Step8EstimatedMaterialConsumptionAndCostDesign(FormMain main)
        {
            InitializeComponent();
            this.main = main;
            dgv_Load();
            Load();
            GetData();
            cmdCasing.SelectedChangedEvent += cmdCasing_SelectedChangedEvent;
            casing = casingList.FirstOrDefault(m => m.SpudNo == cmdCasing.SelectedText);
        }
        void Load()
        {
            List<KeyValuePair<string, string>> spudCom = casingList.Select(m =>
                new KeyValuePair<string, string>(m.SpudNo, m.SpudNo)
            ).ToList();
            cmdCasing.Source = spudCom;
            cmdCasing.SelectedIndex = 0;
        }
        private void dgv_Load()
        {
            List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "SpudNo", HeadText = "SpudNo", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "BitSize", HeadText = "Bit Size (mm)", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "WellSection", HeadText = "WellSection", Width = 200, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "WellVolume", HeadText = "WellVolume", Width = 150, WidthType = SizeType.Absolute });
            lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Consumption", HeadText = "Consumption", Width = 150, WidthType = SizeType.Absolute });
            this.dgv.Columns = lstCulumns;
        }
        void GetData()
        {
            for (int i = 0; i < casingList.Count; i++)
            {
                var casingone = casingList[i];
                var deapth = depthList.Where(n => n.SpudNo.Equals(casingone.SpudNo)).FirstOrDefault();
                if (deapth == null)
                {
                    deapth = new DeapthWell();
                    depthList.Add(deapth);
                }
                deapth.SpudNo = casingone.SpudNo;
                deapth.BitSize = casingone.BitSize;
                double huan2 = ((double.Parse(casingone.BitSize) * double.Parse(casingone.BitSize) - 0) * 3.1415926 / 4000);
                double well = 0;
                double well2 = 0;
                if (i == 0)
                {
                    deapth.WellSection = "0 -" + casingone.HoleDepth;
                    cacingsize1 = double.Parse(casingone.CasingSize);
                    duan1 = double.Parse(casingone.HoleDepth);
                    well = (huan2 * duan1 / 1000);
                    deapth.WellVolume = well.ToString("0.##");
                    deapth.Consumption = (well + 60 + 12).ToString("0.##");
                }
                else if (i == 1)
                {
                    deapth.WellSection = casingList[0].HoleDepth + " -" + casingone.HoleDepth;
                    double huan1 = (cacingsize1 * cacingsize1 - 0) * 3.1415926 / 4000;
                    cacingsize2 = double.Parse(casingone.CasingSize);
                    duan2 = double.Parse(casingone.HoleDepth);
                    var duan = duan2 - duan1;
                    well = (huan1 * duan1 / 1000);
                    well2 = (huan2 * duan / 1000);
                    double consumption1 = (well + well2) + 60 * 2 + 15;
                    deapth.WellVolume = (well + well2).ToString("0.##");
                    deapth.Consumption = consumption1.ToString("0.##");
                }
                else if (i == 2)
                {
                    deapth.WellSection = casingList[1].HoleDepth + " -" + casingone.HoleDepth;
                    double huan1 = (cacingsize2 * cacingsize2 - 0) * 3.1415926 / 4000;
                    var duan3 = double.Parse(casingone.HoleDepth) - duan2;
                    well = (huan1 * duan2 / 1000);
                    well2 = (huan2 * duan3 / 1000);
                    double consumption1 = (well + well2) + 60 * 3 + 15;
                    deapth.WellVolume = (well + well2).ToString("0.##");
                    deapth.Consumption = consumption1.ToString("0.##");
                }
                deapth.DensityEx = lbldensity.InputText;
                deapth.Volume = txtvolome.InputText;
                deapth.Barite = txtbarite.InputText;
            }
            this.dgv.DataSource = depthList;
        }
        private void cmdCasing_SelectedChangedEvent(object sender, EventArgs e)
        {
            //获取对应的套管
            //btnsave_BtnClick(null, null);
            string spud = cmdCasing.SelectedText;
            casing = casingList.FirstOrDefault(m => m.SpudNo == spud);
            InitData(casing);
        }
        double duan1 = 0;
        double duan2 = 0;
        double cacingsize1 = 0;
        double cacingsize2 = 0;
        void InitData(Casing casing)
        {
            if (casing != null)
            {
                var deapth = depthList.Where(n => n.SpudNo.Equals(casing.SpudNo)).FirstOrDefault();
                if (deapth != null)
                {
                    lblbitsize.Text = deapth.BitSize;      //BitSize赋值
                    lblcementing.Text = deapth.WellSection;      //BitSize赋值
                    lblwell.Text = deapth.WellVolume;
                    txtconsumption.InputText = deapth.Consumption;
                }
            }
        }
        private void btnsave_BtnClick(object sender, EventArgs e)
        {
            foreach (var item in depthList)
            {
                item.DensityEx = lbldensity.InputText;
                item.Barite = txtbarite.InputText;
                item.Volume = txtvolome.InputText;
            }
            Session.DepthWellModelList = depthList;
        }
        private void btnUp_BtnClick(object sender, EventArgs e)
        {
            main.AddControl(new Step7(main));
        }
        private void btnDown_BtnClick(object sender, EventArgs e)
        {
            btnsave_BtnClick(null, null);
            main.AddControl(new Step9BitTypesDesign(main));
        }
        private void btnword_BtnClick(object sender, EventArgs e)
        {
            var depthListOld = main.projects.Where(n => n.TableName == TableNames.depthWell.ToString()).FirstOrDefault();
            if (depthListOld != null)
            {
                Session.DepthWellModelList = JsonConvert.DeserializeObject<List<DeapthWell>>(depthListOld.TableData);
                depthList = Session.DepthWellModelList;
                dgv.DataSource = Session.DepthWellModelList;
            }
        }
        private void btnsavetodb_BtnClick(object sender, EventArgs e)
        {
            btnsave_BtnClick(null, null);
            SQLiteDataReader reader = main.sqliteHelper.ReadFullTable("SessionData");
            var projects = EntityTranslate.Translate<SessionData>(reader);
            var FormationPressure = projects.Where(n => n.TableName == TableNames.depthWell.ToString()).FirstOrDefault();
            if (FormationPressure != null)
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.depthWell.ToString(), JsonConvert.SerializeObject(Session.DepthWellModelList));
            else
                SQLiteHelper.SaveData(main.sqliteHelper, TableNames.depthWell.ToString(), JsonConvert.SerializeObject(Session.DepthWellModelList), 1);
            FrmTips.ShowTipsSuccess(new FormMain(), "数据保存成功！");
        }
        private void StripMenuDel_Click(object sender, EventArgs e)
        {
            if (dgv.SelectRow != null)
            {
                int row = dgv.SelectRow.RowIndex;
                if (row > -1)
                {
                    depthList.RemoveAt(row);
                    this.dgv.DataSource = depthList;
                    return;
                }
            }
            FrmTips.ShowTipsWarning(this.FindForm(), "请先选择要删除的行");
        }
    }
}
